<?php

namespace App\Http\Controllers\Admin;

use App\Models\Fangattr;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class FangAttrController extends Controller
{

    public function index()
    {
        //实例化
        $model = new Fangattr();
        $data = $model->getList();
        return view('admin.fangattr.index', compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = Fangattr::where('pid', 0)->get();
        return view('admin.fangattr.create', compact('data'));
    }

    //文件上传
    public function upfile(Request $request) {
        $pic = config('up.pic');
        if ($request->hasFile('file')) {
            //上传图片到cfg_fangattr_pic
            $new_file_name = $request->file('file')->store('', 'cfg_fangattr_pic');
            if (is_string($new_file_name)) {
                //保存成功
                $pic = '/uploads/fangattr/' . $new_file_name; //完整路径
            }
        }
        return ['status' => 0, 'url' => $pic];
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //表单验证
        $this->validate($request, [
            'name' => 'required',
//            'field_name' => 'required'
        ]);

        //验证通过后，入库并跳转到列表页面
        $postData = $request->except(['_token', 'file']);
        //field_name数据库不能为空，但用户输入可以为空
        $postData['field_name'] = empty($postData['field_name']) ? '' : $postData['field_name'];
        //入库
        Fangattr::create($postData);
        //跳转
        return redirect(route('admin.fangattr.index'))->with(['success' => '添加房源属性成功']);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Fangattr  $fangattr
     * @return \Illuminate\Http\Response
     */
    public function show(Fangattr $fangattr)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Fangattr  $fangattr
     * @return \Illuminate\Http\Response
     */
    public function edit(Fangattr $fangattr)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Fangattr  $fangattr
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Fangattr $fangattr)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Fangattr  $fangattr
     * @return \Illuminate\Http\Response
     */
    public function destroy(Fangattr $fangattr)
    {
        //
    }
}
